package jmine.tec.persist.chinese;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import jmine.tec.utils.register.Receiver;

import org.hibernate.engine.FilterDefinition;

/**
 * Class used to keep record of active chinese wall filters. This is needed to activate filters before query execution by the BaseDAO. If
 * used to register a filter defined on Spring, the mapping bean must depend on the filter registration. If the filter is defined through
 * annotations (recommended), the name is enough.
 * 
 * @author lundberg
 */
public class ChineseWallFilterRegistry implements Receiver<Object> {

    private Set<String> filterNames = new HashSet<String>();

    private Collection<FilterDefinition> filters = new ArrayList<FilterDefinition>();

    /**
     * Registers a collection of filter names or filter definitions
     * 
     * @param registers a filter name collection
     */
    public void register(Collection<? extends Object> registers) {
        for (Object reg : registers) {
            if (reg instanceof String) {
                this.filterNames.add((String) reg);
            } else if (reg instanceof FilterDefinition) {
                FilterDefinition def = (FilterDefinition) reg;
                this.filterNames.add(def.getFilterName());
                this.filters.add(def);
            } else {
                throw new IllegalArgumentException("ChineseWallFilterRegistry must receive either String of FilterDefinition.");
            }
        }
    }

    /**
     * Returns the set of registered filters.
     * 
     * @return String set
     */
    public Set<String> getFilterNames() {
        return this.filterNames;
    }

    /**
     * @return filters
     */
    public Collection<FilterDefinition> getFilters() {
        return this.filters;
    }
}
